package com.example.dormitory_system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormitory_system.domain.dto.student.HolidayStayAddDTO;
import com.example.dormitory_system.domain.dto.student.HolidayStayListDTO;
import com.example.dormitory_system.domain.dto.sysuser.HolidayStayUpdateDTO;
import com.example.dormitory_system.domain.enetiy.HolidayStay;
import com.example.dormitory_system.domain.vo.student.HolidayStayVO;
import com.example.dormitory_system.domain.vo.sysuser.SysHolidayStayVO;
import com.example.dormitory_system.service.HolidayStayService;
import com.example.dormitory_system.mapper.HolidayStayMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author keenan
* @description 针对表【tb_holiday_stay(假期留校申请表)】的数据库操作Service实现
* @createDate 2025-03-26 15:28:42
*/
@Service
public class HolidayStayServiceImpl extends ServiceImpl<HolidayStayMapper, HolidayStay>
    implements HolidayStayService {

    @Autowired
    private HolidayStayMapper holidayStayMapper;

    @Override
    public int addHolidayStay(HolidayStayAddDTO holidayStayAddDTO) {
        // 将 DTO 转换为实体对象
        HolidayStay holidayStay = new HolidayStay();
        BeanUtil.copyProperties(holidayStayAddDTO, holidayStay);
        holidayStay.setStatus(1);

        // 插入新记录
       return holidayStayMapper.insert(holidayStay);

    }

    @Override
    public List<HolidayStayVO> listHolidayStay(HolidayStayListDTO holidayStayListDTO) {
        PageHelper.startPage(holidayStayListDTO.getPageNum(), holidayStayListDTO.getPageSize());
        return holidayStayMapper.selectByStudentId(holidayStayListDTO.getStudentId());
    }

    @Override
    public int deleteHolidayStay(Long studentId, Long holidayStayId) {
        return holidayStayMapper.update(new LambdaUpdateWrapper<HolidayStay>()
                .eq(HolidayStay::getHolidayStayId, holidayStayId)
                .eq(HolidayStay::getStudentId, studentId)
                .eq(HolidayStay::getStatus, 1)
                .set(HolidayStay::getStatus, 4));
    }

    @Override
    public int updateHolidayStay(HolidayStayUpdateDTO holidayStayUpdateDTO) {
        return holidayStayMapper.update(new LambdaUpdateWrapper<HolidayStay>()
                .eq(HolidayStay::getHolidayStayId, holidayStayUpdateDTO.getApprovalOpinion())
                .eq(HolidayStay::getStatus, 1)
                .set(HolidayStay::getStatus, holidayStayUpdateDTO.getStatus())
                .set(HolidayStay::getApprovalOpinion, holidayStayUpdateDTO.getApprovalOpinion()));
    }

    @Override
    public List<SysHolidayStayVO> sysListHolidayStay(Long userId, Integer status,
                                                     String applyname,String number,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return holidayStayMapper.selectHolidayStayList(userId,applyname,number);
    }
}